package com.zzyl.nursing.service.member.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.common.utils.threadlocal.UserThreadLocal;
import com.zzyl.nursing.domain.member.Reservation;
import com.zzyl.nursing.dto.member.ReservationDto;
import com.zzyl.nursing.mapper.member.MemberReservationMapper;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.service.member.MemberReservationService;
import com.zzyl.nursing.vo.member.CreateByTimeVo;
import com.zzyl.nursing.vo.member.ReservationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

import static com.zzyl.common.utils.SecurityUtils.getUserId;

@Service
public class MemberReservationServiceImpl implements MemberReservationService {
    @Autowired
    private MemberReservationMapper memberReservationMapper;

    @Autowired
    private IFamilyMemberService familyMemberService;

    /**
     * 获取取消预约数量
     * @return
     */
    @Override
    public Integer getCancelCount() {
        //1.获取查询时间段的开始时间和结束时间
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        //2.取消状态为2
        Integer status = 2;
        //3.获取线程中的用户
        //Long userId = getUserId();//后端登录获取用户id不适用小程序
        Long userId = UserThreadLocal.getUserId();
        List<Reservation> result = memberReservationMapper.getCancelCount(startTime, endTime,status);
        int size = result.size();
        return size;
    }
    /**
     * 获取剩余预约数量
     * @return
     */
    @Override
    public List<CreateByTimeVo> getCountByTime() {
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        List<CreateByTimeVo> result = memberReservationMapper.getCountByTime(startTime,endTime);
        return result;
    }

    @Override
    public boolean add(ReservationDto dto) {
        //1.将前端参数赋值给实体类
        Reservation reservation = new Reservation();
        BeanUtils.copyProperties(dto, reservation);
        //2.补全数据
        //设置预约状态
        reservation.setStatus(0);
        int row =  memberReservationMapper.add(reservation);
        return row > 0;
    }
    /**
     * 分页查询预约信息
     * @param status
     * @return
     */
    @Override
    public ReservationVo findByPage(Integer pageNum, Integer pageSize, Integer status) {
        //1.开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        //IPage<ReservationVo> ipage = new Page<>(pageNum, pageSize);
        //2.创建分页对象
        List<Reservation> data = memberReservationMapper.findByPage();
        Page<Reservation> p = (Page<Reservation>)data;
        Long total = p.getTotal();
        ReservationVo vo = new ReservationVo(total, 200, "查询成功", data);
        return vo;
    }

    /**
     * 取消预约
     * @param id
     * @return
     */
    @Override
    public boolean cancel(Long id) {
        int row = memberReservationMapper.cancel(id);
        return row > 0;
    }
}
